package com.jnzc.ocr.serv.shop.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.jnzc.ocr.common.utils.CalculateUtils;
import com.jnzc.ocr.common.utils.DateUtils;
import com.jnzc.ocr.common.utils.ValidateUtils;
import com.jnzc.ocr.dependencies.serv.mybatis.MybatisServiceImpl;
import com.jnzc.ocr.serv.shop.entity.*;
import com.jnzc.ocr.serv.shop.filter.ShopCheckFilter;
import com.jnzc.ocr.serv.shop.helper.ShopCheckHelper;
import com.jnzc.ocr.serv.shop.mapper.ShopCheckMapper;
import com.jnzc.ocr.serv.shop.service.*;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.jnzc.ocr.serv.shop.vo.ShopCheckCreateVo;
import com.jnzc.ocr.serv.shop.vo.ShopCheckDetailCreateVo;
import com.jnzc.ocr.serv.shop.vo.ShopProductStockChangeVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @author zhangcm
 * @version 1.0, 2017-09-29
 */
@Service
public class ShopCheckServiceImpl extends MybatisServiceImpl<ShopCheckMapper, ShopCheck, ShopCheckFilter> implements ShopCheckService {

    @Autowired
    private ShopGoodsService shopGoodsService;

    @Autowired
    private ShopGoodsSkuService shopGoodsSkuService;

    @Autowired
    private ShopCheckDetailService shopCheckDetailService;

    @Autowired
    private ShopProductStockService shopProductStockService;

    @Override
    protected void fillEntityWrapper(EntityWrapper<ShopCheck> ew, ShopCheckFilter filter) {
        ShopCheck entity = new ShopCheck();
        entity.setId(filter.getId());
        entity.setShopId(filter.getShopId());
        ew.setEntity(entity);
    }

    @Override
    public ShopCheck find(Long id, Long shopId) {
        ValidateUtils.isId(id);
        ShopCheckFilter filter = new ShopCheckFilter();
        filter.setId(id);
        filter.setShopId(shopId);
        return findNotNull(filter);
    }

    @Override
    @Transactional
    public ShopCheck save(ShopCheckCreateVo vo, Long shopId) {

        List<ShopCheckDetail> detailList = Lists.newArrayList();
        for (ShopCheckDetailCreateVo detailVo : vo.getDetailList()) {
            ShopGoods goods = shopGoodsService.find(detailVo.getProductId(), shopId);
            ShopGoodsSku goodsSku = shopGoodsSkuService.find(detailVo.getSkuId(), detailVo.getProductId(), shopId);
            ShopCheckDetail detail = new ShopCheckDetail();
            detail.setShopId(shopId);
            detail.setProductId(goods.getId());
            detail.setProductName(goods.getName());
            detail.setSkuId(goodsSku.getId());
            detail.setSkuName(goodsSku.getName());
            detail.setOldNum(CalculateUtils.def(goodsSku.getStockNum()));
            detail.setNewNum(CalculateUtils.def(detailVo.getNum()));
            detail.setChangeNum(CalculateUtils.sub(detail.getNewNum(), detail.getOldNum()));
            detailList.add(detail);
        }

        String content = ShopCheckHelper.getContent(detailList);

        ShopCheck obj = new ShopCheck();
        obj.setShopId(shopId);
        obj.setContent(content);
        obj.setRemark(vo.getRemark());
        obj.setCreateTime(DateUtils.getNow());
        this.insert(obj);

        for (ShopCheckDetail detail : detailList) {
            detail.setCheckId(obj.getId());
        }
        shopCheckDetailService.insertList(detailList);

        // 修改库存
        List<ShopProductStockChangeVo> changeList = Lists.newArrayList();
        for (ShopCheckDetail detail : detailList) {
            ShopProductStockChangeVo change = new ShopProductStockChangeVo();
            change.setProductId(detail.getProductId());
            change.setSkuId(detail.getSkuId());
            change.setAddStockNum(detail.getChangeNum());
            changeList.add(change);
        }
        shopProductStockService.updateNum(changeList, shopId);
        return obj;
    }

    @Override
    public Date getLastDate(Long shopId) {
        ShopCheckFilter filter = new ShopCheckFilter();
        filter.setSelectBy("max(create_time) as create_time");
        filter.setShopId(shopId);
        ShopCheck obj = super.find(filter);
        return null == obj ? null : obj.getCreateTime();
    }
}
